package com.example.sonny.dutest;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.cengalabs.flatui.FlatUI;
import com.example.sonny.bean.BookItem;
import com.example.sonny.bean.ChatBean;
import com.example.sonny.com.jialin.chat.Message;
import com.example.sonny.dutest.interfaces.BaseActivityI;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.Date;

public class SingleBookActivity extends BaseActivity implements BaseActivityI {

    private ImageView book_iv;
    private TextView name_tv;
    private  TextView intr_tv;
    private Button btn_borrow;
    private Button btn_contact;


    private String imgUrl;
    private String name;
    private String publisher;
    private String intr;
    private BookItem book;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();

        book = (BookItem)extras.getSerializable("book");
        imgUrl = book.getImgUrl();
        name = book.getName();
        publisher = "working on it";
        setContentView(R.layout.activity_single_book);

        preInit();

        initView();

        initData();

        initListener();


    }

    @Override
    public void preInit() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setBackgroundDrawable(FlatUI.getActionBarDrawable(this, FlatUI.GRASS, false));
    }

    public void initView() {
        book_iv = (ImageView)findViewById(R.id.book_image);
        name_tv = (TextView)findViewById(R.id.name_tv);
        intr_tv = (TextView)findViewById(R.id.intr_tv);
        btn_borrow = (Button)findViewById(R.id.btn_borrow);
        btn_contact = (Button)findViewById(R.id.btn_contact);
    }

    public void initData() {
        //book_iv.setImageResource(imgUrl);//TODO set correct image
        ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.displayImage(imgUrl,book_iv);
        name_tv.setText(name);
        intr_tv.setText(publisher);
    }

    public void initListener() {
        btn_borrow.setOnClickListener(this);
        btn_contact.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_single_book, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if(id == android.R.id.home) {
            // app icon in action bar clicked; goto parent activity.
            this.finish();
            return true;
        }

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.btn_borrow:
                toast("该模块仍在开发");
                break;
            case R.id.btn_contact:
                doContact();
                break;
        }

    }

    private void doContact() {
        ChatBean chat = new ChatBean();
        Message message = new Message(Message.MSG_TYPE_TEXT, Message.MSG_STATE_SUCCESS, "Tom", "avatar", "Jerry", "avatar", "test Hohoho", false, true, new Date(System.currentTimeMillis() - (1000 * 60 * 60 * 24) * 8));
        chat.setTarget("Tom");
        chat.setLatest(message);
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putSerializable("chat", chat);
        intent.putExtras(bundle);
        intent.setClass(SingleBookActivity.this, ChatMainActivity.class);
        startActivity(intent);
    }
}
